package com.weimob.mcloud.wechatwork.customer.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.weimob.arch.artemis.client.api.consume.ArtemisMessage;
import com.weimob.arch.artemis.client.api.consume.ArtemisMessages;
import com.weimob.arch.artemis.client.spring.annotation.ArtemisMessageListener;
import com.weimob.arch.artemis.client.spring.annotation.Subscribe;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.service.group.request.CustomerGroupExternalRequest;
import com.weimob.mcloud.wechatwork.core.service.group.response.CustomerGroupExternalResponse;
import com.weimob.mcloud.wechatwork.customer.domain.ArtemisConstant;
import com.weimob.mcloud.wechatwork.customer.mq.model.RetailStoreDeleteMqModel;
import com.weimob.mcloud.wechatwork.customer.third.PrivateSphereThird;
import com.weimob.mcloud.wechatwork.customer.third.core.CustomerGroupExportServiceProxy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenliuzhu
 * @desc 零售门店删除消费者
 * @date 2021-10-11
 */
@Component
@Slf4j
public class RetailStoreDeleteConsumer {

    @Resource
    CustomerGroupExportServiceProxy customerGroupExportServiceProxy;

    @Resource
    private PrivateSphereThird privateSphereThird;

    /**
     * 零售门店删除消费者
     * 数据刷新同步
     *
     * @param result
     */
    @ArtemisMessageListener(
            value = @Subscribe(topic = ArtemisConstant.Topic.WECHATWORK_CUSTOMER_DELETE_STORE_MSG, user = ArtemisConstant.USER))
    public void processDeleteStore(ArtemisMessages result) {
        if (result == null) {
            return;
        }
        List<ArtemisMessage> consumerRecords = result;
        log.info(" WOS零售门店删除消费者 ArtemisMessages:{}", JSONObject.toJSONString(result));
        if (CollectionUtils.isEmpty(consumerRecords)) {
            return;
        }
        consumerRecords.forEach(consumerRecord -> {
            log.info(" WOS零售门店删除消费者 consumerRecord:{}", consumerRecord);
            RetailStoreDeleteMqModel retailStoreDeleteMqModel = null;
            try {
                retailStoreDeleteMqModel = JSON.parseObject(consumerRecord.getValue(), RetailStoreDeleteMqModel.class);
                if (retailStoreDeleteMqModel == null) {
                    return;
                }
                Long vid = retailStoreDeleteMqModel.getVid();
                String bosId = retailStoreDeleteMqModel.getBosId();
                // 查私域
                PrivateSphere privateSphere = privateSphereThird.fetchByBosId(Long.parseLong(bosId));
                // 查
                CustomerGroupExternalRequest customerGroupExternalRequest = new CustomerGroupExternalRequest();
                customerGroupExternalRequest.setPrivateSphere(privateSphere);
                customerGroupExternalRequest.setVid(String.valueOf(vid));
                List<CustomerGroupExternalResponse> externalResponseList = customerGroupExportServiceProxy.batchFetchCustomerGroupExternal(customerGroupExternalRequest);
                // 标记为删除
                if(!CollectionUtils.isEmpty(externalResponseList)){
                    List<CustomerGroupExternalRequest> externalRequestList = new ArrayList<>();
                    for (CustomerGroupExternalResponse externalResponse : externalResponseList) {
                            CustomerGroupExternalRequest externalRequest = new CustomerGroupExternalRequest();
                            externalRequest.setPrivateSphere(privateSphere);
                            externalRequest.setGroupId(externalResponse.getGroupId());
                            externalRequest.setVid(externalResponse.getVid());
                            externalRequest.setType(2);
                            externalRequest.setDeleted(true);
                            externalRequestList.add(externalRequest);
                    }
                    customerGroupExportServiceProxy.saveAllCustomerGroupExternal(externalRequestList);
                }
            } catch (Exception e) {
                log.error("WOS零售门店删除消费者 删除失败 consumerRecord:{}, cause by:{}", consumerRecord, e.getMessage(), e);
            }
        });
    }
}
